package ast;

import out.Out;
import mem.interfaces.IMem;
import environnement.interfaces.IEnv;
import exceptions.LogoException;
import ast.interfaces.IAST;
import ast.interfaces.IASTif;

public class ASTif implements IASTif{

	private IAST condition;
	private IAST c1;
	private IAST c2;
	
	public ASTif(IAST condition, IAST c1, IAST c2) {
		this.condition = condition;
		this.c1 = c1;
		this.c2 = c2;
	}

	@Override
	public Object eval(Out o, IEnv e, IMem m) throws LogoException{
		Float eval_condition = (Float) condition.eval(o, e, m);
		
		//Clone de l'environnement
		IEnv env_eval = e.clone();
		
		//Evaluation du if
		if(eval_condition == 1){
			return ((ASTprog) c1).eval(o, env_eval, m);
		}
		else {
			return ((ASTprog) c2).eval(o, env_eval, m);

		}
	}
	
	public String toTreeString(String ident,IEnv e,IMem m) {
		String ret ="";
		ret+= ident + "IF " + getCondition().toTreeString(ident, e, m) + " ";
		ret+= getC1().toTreeString("", e, m) + " ";
		ret+= getC2().toTreeString("", e, m);
		return ret;
	}

	public IAST getCondition() {
		return condition;
	}

	public void setCondition(IAST condition) {
		this.condition = condition;
	}

	public IAST getC1() {
		return c1;
	}

	public void setC1(IAST c1) {
		this.c1 = c1;
	}

	public IAST getC2() {
		return c2;
	}

	public void setC2(IAST c2) {
		this.c2 = c2;
	}
	
	
}
